<?php

if (!defined('WPSR_ACCESS')) { exit; }



/**
 * WPsrAdm class.
 * 
 * @extends WPsr
 */
class WPsrAdm extends WPsr
{



	/**
	 * __construct function.
	 *
	 * @access private
	 * @return void
	 */
	function __construct() {
		if ($_GET['m'] != '') {
			$msgs = explode("x", $_GET['m']);
			$this->message = array_merge($this->message, $msgs);
		}
		if ($_GET['e'] != '') {
			$errs = explode("x", $_GET['e']);
			$this->error = array_merge($this->error, $errs);
		}
	}




	/**
	 * __destruct function.
	 * 
	 * @access private
	 * @return void
	 */
	function __destruct() {

	}




	/**
	 * getErrorUrl function.
	 * 
	 * @access public
	 * @return string
	 */
	function getErrorUrl() {
		$errs = implode('x', $this->error);
		return $errs;
	}





	/**
	 * getMessageUrl function.
	 * 
	 * @access public
	 * @return string
	 */
	function getMessageUrl() {
		$msgs = implode('x', $this->message);
		return $msgs;
	}

	
	
	
	function getAlbList() {
		
		include(WPSR_CACHE."inc.alblist.php");
		
		return $album;
	
	}
	
	
	
	
	function updateAlbList() {
	
		$handle = fopen(WPSR_CACHE."inc.alblist.php", "w");
		
		$allalb = $this->getAlbums(1, 0);
		
		$text 	=	'<?php
								if (!defined("WPSR_ACCESS")) { exit; }
								$album = array(';
		
		foreach ($allalb as $alb) {
			$text .= $alb->aid.' => "'.str_repeat('—', $alb->intend).' '.$alb->title.'",';
		}
		$text 	.= '); 
								?>';
		
		fwrite($handle, $text);

		fclose($handle);
	
	}
	



	/**
	 * deletePicture function.
	 * 
	 * @access public
	 * @param int $pid
	 * @return void
	 */
	function deletePicture($pid) {
		$picture = $this->getPicById($pid);
		if ( $this->checkObj($picture->pid) ) {
			$this->error[] = '12';
		}

		if ( count($this->error) < 1 && count($this->message) < 1) {
			$this->dodeletePic($picture->pid, $picture->path, $picture->file);
		}
	}





	/**
	 * deletePictures function.
	 * 
	 * @access public
	 * @param array $pictures
	 * @return void
	 */
	function deletePictures($pictures) {
		global $wpdb;

		$picslist = implode(',', $pictures);
		$q = $wpdb->prepare("SELECT id FROM ".WPSR_DB_R." WHERE id IN ($picslist) AND level > $this->user");
		$result = $wpdb->get_results($q);
		foreach ($result as $res) {
			if (in_array($res->id, $pics)) {
				$this->error[] = '0';
			}
		}
		if ( count($this->error) < 1 && count($this->message) < 1) {
			foreach ($pictures as $pics) {
				$pic = $this->getPicById($pics);
				$this->dodeletePic($pic->pid, $pic->path, $pic->file);
			}
		}

	}





	/**
	 * dodeletePic function.
	 * 
	 * @access public
	 * @param int $pid
	 * @param string $path
	 * @param string $file
	 * @return void
	 */
	function dodeletePic($pid, $path, $file) {
		global $wpdb;
		require_once "wpsr.file.php";
		
		if (WPsrFile::deleteFile($path, $file)) {		
			$q = $wpdb->query("DELETE FROM ".WPSR_DB_P." WHERE pid = $pid LIMIT 1");
			$q = $wpdb->query("DELETE FROM ".WPSR_DB_R." WHERE id = $pid LIMIT 1");
			$this->deletePicComs($pid);
			$this->message[] = '16';
		}
		else {
			$this->message[] = '17';
		}
	}




	function deletePicComs($pid) {
		global $wpdb;
		
		$q = "SELECT comment_ID FROM $wpdb->comments WHERE pid = $pid";
		$q = $wpdb->prepare($q);
		$result = $wpdb->get_results($q);
		if ($result && count($result) > 0) {
			foreach ($result as $com) {
				wp_delete_comment($com->comment_ID);
			}		
		}
	}




	/**
	 * editPictures function.
	 * 
	 * @access public
	 * @return void
	 */
	function editPictures() {
		global $wpdb;

		if ($_POST['docaption'] == 1) {
			$caption = stripslashes($_POST['caption']);
			$caption = htmlspecialchars($caption);
		}
		if ($_POST['dolevel'] == 1) {
			$level = (int) $_POST['level'];
		}
		if ($_POST['dohits'] == 1) {
			$hits = (int) $_POST['hits'];
		}
		if ($_POST['doparent'] == 1) {
			$parent = (int) $_POST['parent'];
			$parent = $this->getAlbById($parent);
			$parent->isOwner = $this->getOwner($parent->owner);
			if ( $this->checkObj($parent) || $parent->id == 1 ) {
				$this->error[] = '11';
			}
		}

		$pics = $_POST['pic'];
		$picslist = implode(',', $pics);
		$q = $wpdb->prepare("SELECT id FROM ".WPSR_DB_R." WHERE id IN ($picslist) AND level > $this->user");
		$result = $wpdb->get_results($q);
		foreach ($result as $res) {
			if (in_array($res->id, $pics)) {
				$this->error[] = '0';
			}
		}

		if ( count($this->error) < 1 && count($this->message) < 1) {

			if ($_POST['dolevel'] == 1) {
				$p = $wpdb->query("UPDATE ".WPSR_DB_R." SET level = $level WHERE id IN ($picslist)");
			}
			if ($_POST['docaption'] == 1 || $_POST['dohits'] == 1 || $_POST['doparent'] == 1) {
				$query = "UPDATE ".WPSR_DB_P." SET";
				$subquery = "";
				$set = array();
				if ($_POST['docaption'] == 1) {
					$subquery .= " caption = '$caption' ,";
				}
				if ($_POST['dohits'] == 1) {
					$subquery .= " hits = $hits ,";
				}
				if ($_POST['doparent'] == 1) {
					$subquery .= " aid = $parent->aid ,";
				}
				$subquery = substr($subquery, 0, -1);
				$query .= $subquery." WHERE pid IN ($picslist)";

				$q = $wpdb->prepare($query);
				$result = $wpdb->query($q);

				$this->message[] = '14';
			}
		}
		else {
			$this->message[] = '15';
		}
	}





	/**
	 * editPicture function.
	 * 
	 * @access public
	 * @return void
	 */
	function editPicture() {
		global $wpdb;

		$updateID = (int) $_POST['pid'];
		$picture = $this->getPicById($updateID);
		if ( $this->checkObj($picture->pid) ) {
			$this->error[] = '10';
		}
		$caption = stripslashes($_POST['caption-'.$updateID]);
		$caption = htmlspecialchars($caption);
		$parent = (int) $_POST['parent-'.$updateID];
		$parent = $this->getAlbById($parent);
		$parent->isOwner = $this->getOwner($parent->owner);
		if ( $this->checkObj($parent) ) {
			$this->error[] = '11';
		}
		$level = (int) $_POST['level-'.$updateID];
		$hits = (int) $_POST['hits-'.$updateID];

		if ( count($this->error) < 1 && count($this->message) < 1) {
			$aid = $parent->aid;
			$wpdb->update(WPSR_DB_R, compact('level'), array('id' => $updateID));
			$wpdb->update(WPSR_DB_P, compact('caption', 'hits', 'aid'), array('pid' => $updateID));
			$this->message[] = '12';
		}
		else {
			$this->message[] = '13';
		}
	}





	/**
	 * AlbumCover function.
	 * 
	 * @access public
	 * @return void
	 */
	function AlbumCover() {
		global $wpdb;

		$picID = (int) $_POST['pid'];
		$picture = $this->getPicById($picID);
		if ( $this->checkObj($picture->pid) ) {
			$this->error[] = '13';
		}
		$album = (int) $_POST['cover-'.$picID];
		$album = $this->getAlbById($album);
		$album->isOwner = $this->getOwner($album->owner);
		if ( $this->checkObj($album) ) {
			$this->error[] = '6';
		}
		if ( count($this->error) < 1 && count($this->message) < 1) {
			$cover = $picture->path.$picture->file;
			$wpdb->update(WPSR_DB_A, compact('cover'), array('aid' => $album->id));
			$this->message[] = '3';
		}
		else {
			$this->message[] = '4';
		}
	}





	/**
	 * editAlbum function.
	 * 
	 * @access public
	 * @return void
	 */
	function editAlbum() {
		global $wpdb, $user_ID;

		$updateID = (int) $_POST['aid'];
		$album = $this->getAlbById($updateID);
		if ( $this->checkObj($album->aid) ) {
			$this->error[] = '6';
		}
		$title = stripslashes($_POST['title-'.$updateID]);
		$title = htmlspecialchars($title);
		if ($title == "") {
			$this->message[] = '2';
		}
		$uri = $this->adminAlbumUri($_POST['title-'.$updateID], $updateID);
		$desc = stripslashes($_POST['desc-'.$updateID]);
		$description = htmlspecialchars($desc);
		$meta = stripslashes($_POST['meta-'.$updateID]);
		$meta = htmlspecialchars($meta);
		
		$parent = (int) $_POST['parent-'.$updateID];
		$parent = $this->getAlbById($parent);
		$parent->isOwner = $this->getOwner($parent->owner);
		if ( $this->checkObj($parent) ) {
			$this->error[] = '7';
		}
		$com = (int) $_POST['comments-'.$updateID];
		$level = (int) $_POST['level-'.$updateID];

		if ( count($this->error) < 1 && count($this->message) < 1) {
			$paid = $parent->id;
			$wpdb->update(WPSR_DB_R, compact('level'), array('id' => $updateID));
			$wpdb->update(WPSR_DB_A, compact('paid', 'title', 'uri', 'description', 'com', 'meta'), array('aid' => $updateID));
			$this->updateAlbList();
			$this->message[] = '3';
		}
		else {
			$this->message[] = '4';
		}
	}





	/**
	 * deleteAlbum function.
	 * 
	 * @access public
	 * @param int $aid
	 * @return void
	 */
	function deleteAlbum($aid) {
		global $wpdb;

		if ( $this->checkObj($aid) ) {
			$this->error[] = '9';
		}
		else {
			/**
			 * get pictures according to userlevel
			 * check for hidden pics
			 */
			$pictures = $this->getPicturesByAid($aid, false);
			$deletepics = array();
			$nohiddenpics = true;
			foreach ($pictures as $pics) {
				/**
				 * hidden picture detected
				 */
				if ($pics->level > $this->user) {
					$nohiddenpics = false;
				}
				else {
					$deletepics[] = array('id' => $pics->id, 'path' => $pics->path, 'file' => $pics->file);
				}
			}
			/**
			 * delete pictures
			 */
			foreach ($deletepics as $pic) {
				$this->dodeletePic($pic['id'], $pic['path'], $pic['file']);
			}
			/**
			 * only if no hidden pics, else drop warning
			 */
			if ($nohiddenpics) {
				/**
				 * delete album from DB -- wpsr_alb, wpsr_ref
				 */
				$q = $wpdb->query("DELETE FROM ".WPSR_DB_A." WHERE aid = $aid LIMIT 1");
				$q = $wpdb->query("DELETE FROM ".WPSR_DB_R." WHERE id = $aid LIMIT 1");
				/**
				 * move child albums to root -- update paid = 1 where paid = aid
				 */
				$q = $wpdb->query("UPDATE ".WPSR_DB_A." SET paid = 1 WHERE paid = $aid");
				
				$this->updateAlbList();
			}
		}
	}





	
	/**
	 * addAlbum function.
	 * 
	 * @access public
	 * @return void
	 */
	function addAlbum() {
		global $wpdb, $user_ID;

		$updateID = (int) $_POST['aid'];
		$title = stripslashes($_POST['title-'.$updateID]);
		$title = htmlspecialchars($title);
		if ($title == "") {
			$this->message[] = '2';
		}
		$uri = $this->adminAlbumUri($_POST['title-'.$updateID]);
		$desc = stripslashes($_POST['desc-'.$updateID]);
		$description = htmlspecialchars($desc);
		$meta = stripslashes($_POST['meta-'.$updateID]);
		$meta = htmlspecialchars($meta);
		$parent = (int) $_POST['parent-'.$updateID];

		$parent = $this->getAlbById($parent);
		$parent->isOwner = $this->getOwner($parent->owner);
		if ( $this->checkObj($parent) ) {
			$this->error[] = '7';
		}
		$com = (int) $_POST['comments-'.$updateID];
		$level = (int) $_POST['level-'.$updateID];

		if ( count($this->error) < 1 && count($this->message) < 1) {
			$date = date('Y-m-d H:m:s');
			$id = NULL;
			$type = 1;
			$owner = $user_ID;
			$ord = 1000;
			$paid = $parent->id;
			$wpdb->insert(WPSR_DB_R, compact('id', 'type', 'date', 'owner', 'level'));
			$aid = (int) $wpdb->insert_id;
			$wpdb->insert(WPSR_DB_A, compact('aid', 'paid', 'title', 'uri', 'description', 'ord', 'com', 'meta'));
			
			$this->updateAlbList();
			
			$this->message[] = '3';
		}
		else {
			$this->message[] = '4';
		}
	}





	/**
	 * checkForChild function.
	 * 
	 * @access public
	 * @param int $id
	 * @return void
	 */
	function checkForChild($id) {
		global $wpdb;
		$q = $wpdb->prepare("SELECT aid FROM ".WPSR_DB_A." WHERE paid = $id LIMIT 1");
		$row = $wpdb->get_row($q);
		if ( $row->aid ) {
			return true;
		}
		else {
			return false;
		}
	}





	/**
	 * moveAlbum function.
	 * 
	 * @access public
	 * @param int $id
	 * @param int $paid
	 * @param string $direction
	 * @return void
	 */
	function moveAlbum($id, $paid, $direction) {
		global $wpdb;

		$q = $wpdb->prepare("SELECT aid FROM ".WPSR_DB_A." WHERE paid = $paid ORDER BY ord DESC");
		$result = $wpdb->get_results($q);

		$val = array();
		$order = array();
		foreach ($result as $alb) {
			$val[] = $alb->aid;
		}
		$val = array_reverse($val);
		$key = array_search($id, $val);
		if (count($val) > 1) {
			if ($direction == 'up') {
				if ( $key < count($val)-1 ) {
					for ($i=0; $i < count($val); $i++) {
						if ($i == $key) {
							$order[$i] = $i+1;
						}
						elseif ($i == $key+1) {
							$order[$i] = $i-1;
						}
						else {
							$order[$i] = $i;
						}
					}
				}
			}
			elseif ($direction == 'down') {
				if ( $key > 0 ) {
					for ($i=0; $i < count($val); $i++) {
						if ($i == $key) {
							$order[$i] = $i-1;
						}
						elseif ($i == $key-1) {
							$order[$i] = $i+1;
						}
						else {
							$order[$i] = $i;
						}
					}
				}
			}
			if (count($order) > 0) {
				for ($i=0; $i < count($val); $i++) {
					$ord = $order[$i];
					$wpdb->update(WPSR_DB_A, compact('ord'), array('aid' => $val[$i]));
				}
			}
		}
	}





	/**
	 * adminAlbumUri function.
	 * 
	 * @access private
	 * @param string $uri
	 * @param int $edit. (default: 0)
	 * @return string $uri
	 */
	function adminAlbumUri($uri, $edit = 0) {
		global $wpdb;

		$uri = sanitize_title($uri);
		$q = $wpdb->prepare("SELECT uri, aid FROM ".WPSR_DB_A." WHERE uri = '$uri' LIMIT 1");
		$obj = $wpdb->get_row($q);
		if ($edit == 0 && $obj->uri) {
			$this->message[] = '1';
		}
		elseif ($edit != $obj->aid && $obj->uri) {
			$this->message[] = '1';
		}

		return $uri;
	}





	/**
	 * AdmSecurityManager function.
	 * 
	 * @access public
	 * @return void
	 */
	function AdmSecurityManager() {
		$this->SecurityManager();
		if ($this->obj->isOwner == 0) {
			$this->error[] = '8';
		}
	}





	/**
	 * getShortDate function.
	 * 
	 * @access public
	 * @param mixed $date
	 * @return mixed $date
	 */
	function getShortDate($date) {
		$date = substr($date, 0, 10);
		$date = strtr($date, '-', '/');
		return $date;
	}





	/**
	 * getdate function.
	 * 
	 * @access public
	 * @param mixed $date
	 * @return mixed $date
	 */
	function getdate($date) {
		$date = strtr($date, '-', '/');
		return $date;
	}





	/**
	 * adminInsertPics function.
	 * 
	 * @access public
	 * @return void
	 */
	function adminInsertPics($mode = "new") {
		require_once "wpsr.file.php";
		global $user_ID;

		ob_start();
		if ($mode == "new") {
			$wfiles = WPsrFile::getWaitingFiles($user_ID);		
		}
		elseif ($mode == "reg") {
			$wfiles = WPsrFile::getRegenerateFiles($user_ID);
		}
?>
		<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr>
		<td>
		<table class="widefat">
		<tbody style="height:500px;overflow-y:scroll;overflow-x:hidden;" id="inserthere">
			<tr>
				<td style="border-bottom:none;height:100%;"> </td>
			</tr>
		</tbody>
		</table>
		</td>
		<td valign="top" style="padding-left:25px;width:300px;">
			<span style="display:inline-block;width:100px;">Total files: </span><span id="wpsrtf"><?php echo count($wfiles); ?></span><br />
			<span style="display:inline-block;width:100px;">Files done: </span><span id="wpsrfd">0</span><br />
			<span style="display:inline-block;width:100px;">Files to go: </span><span id="wpsrfg"><?php echo count($wfiles); ?></span>
			<br /><br />
			<div style="width:200px;height:15px;border:1px solid #CCC; background-color:#EEE">
				<div id="insertbar" style="height:15px;background-color:green;width:1px;"></div>
			</div> <span id="insertpercent">0</span> %
			<br /><br />
			Please be patient, generating thumbnails eats up your server and can take forever!
		</td>
		</tr>
		</table>
<?php
		/**
		 * make filepath -> Session
		 */
		if (count($wfiles) > 0) {
			if ($mode == "new") {
				$path = $user_ID.date('ymdH');
				if (WPsrFile::checkPicDirs($path) == true) {
					$path = $path.'/';
?>
					<script type="text/javascript" charset="utf-8">
						insertcount = 0;
						inserttogo = <?php echo count($wfiles); ?>;
						inserttotal = <?php echo count($wfiles); ?>;
						WPSR_insertpics('<?php echo $path; ?>');
					</script>
<?php
				}
				else {
					/**
					 * TODO: als Error ausgeben
					 */
					echo "could not create directories.";
				}			
			}
			elseif ($mode == "reg") {
?>
					<script type="text/javascript" charset="utf-8">
						insertcount = 0;
						inserttogo = <?php echo count($wfiles); ?>;
						inserttotal = <?php echo count($wfiles); ?>;
						WPSR_regenratepics();
					</script>
<?php			
			}
		}

		/**
		 * Stop buffering
		 */
		ob_end_flush();
	}


	
	
	
	/**
	 * adminFooter function.
	 * 
	 * @access public
	 * @return void
	 */
	public function adminFooter() {
?>
		<div class="tablenav" id="WPSR_footer">
			<strong>WP Showroom Gallery</strong> <?php echo WPSR_VERSION; ?> - &copy; <a href="mailto:michi@animalbeach.net">Michael Naab</a> 2008 - <a href="http://www.animalbeach.net">http://www.animalbeach.net</a>
		</div>
<?php
	}





	/**
	 * getAdminMessage function.
	 * 
	 * @access public
	 * @return void
	 */
	public function getAdminMessage() {
		$msgs = array_merge(array_unique($this->err()), array_unique($this->msg()));
		if (count($msgs) > 0) {
			echo '<div id="message" class="updated fade">';
			foreach ($msgs as $msg) {
				echo '<p><strong>'.$msg.'</strong></p>';
			}
			echo '</div>';
		}
	}





	/**
	 * adminManagePictureOutput function.
	 * 
	 * @access public
	 * @param object $wpsr
	 * @return void
	 */
	function adminManagePictureOutput($wpsr, $album) {
?>
		<h2>Showroom - Manage Pictures</h2>
		<div class="tablenav">
			<div class="alignleft">
				<form name="filter" method="get" action="" style="display:inline;" />
				<input name="event" type="hidden" value="filter" />
				<input type="hidden" name="page" value="WPshowroom/wpsr.php" />
				Filter: 
				<select id="faid" name="faid">
					<option value="1"><?php echo WPSR_BASE_NAME; ?></option>
<?php
		foreach ($album as $aid => $title) {
			echo "<option value='$aid'";
			if ($aid == $wpsr->obj->id) {
				echo " selected='selected'";
			}
			echo ">".$title."</option>";
		}
?>
				</select>
				<input id="" class="button-secondary" type="submit" name="album" value="Album"/> 
				<input id="" class="button-secondary" type="submit" name="picture" value="Pictures"/>
				</form>
			</div>
			<div class="alignright">
				<input class="button-secondary" type="button" name="addpic" value="+ Upload Pictures" onclick="WPSR_goto('<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=WPsr-pictures')"/>
				<input class="button-primary" type="button" name="" id="" value="+ Manage Galleries" onclick="WPSR_goto('<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=WPshowroom/wpsr.php')"/>
			</div>
		</div>

		<form name="editPictures" method="post" action="../wp-content/plugins/WPshowroom/post.php" />
		<input name="event" type="hidden" value="editPictures">
		<input name="aid" type="hidden" value="<?php echo $wpsr->obj->id; ?>">

		<table class="widefat">
			<thead>
				<tr>
					<th id="cb" class="manage-column column-cb check-column" style="" scope="col">
						<input type="checkbox"/>
					</th>
					<th scope="col">ID</th>
					<th scope="col">Picture</th>
					<th scope="col">Caption</th>
					<th scope="col">Owner</th>
					<th scope="col">Level</th>
					<th scope="col"># Hits</th>
					<th scope="col" style="text-align:right;"><input id="WPSR-mepb" class="button-secondary" style="padding:2px 8px; margin:1px" type="button" value="+ Mass Edit Pictures" onclick="WPSR_peoptions(<?php echo $wpsr->obj->id; ?>,this);blur();"/></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th id="cb" class="manage-column column-cb check-column" style="" scope="col">
						<input type="checkbox"/>
					</th>
					<th scope="col">ID</th>
					<th scope="col">Picture</th>
					<th scope="col">Caption</th>
					<th scope="col">Owner</th>
					<th scope="col">Level</th>
					<th scope="col"># Hits</th>
					<th scope="col">&nbsp;</th>
				</tr>
			</tfoot>
		<tbody>
			<tr class="WPSR_close WPSR_bbb" id="wpsr-edit-all">
				<td colspan="2">&nbsp;</td>
				<td colspan="6">
					<div id="wpsr-edit-res-all" />
				</td>
			</tr>
<?php
		foreach ($wpsr->pictures as $pic) {
?>
			<tr id="wpsr-<?php echo $pic->id ?>" class="WPSR_tr WPSR_tr_sel">
				<th class="check-column" scope="row">
					<input type="checkbox" value="<?php echo $pic->id; ?>" name="pic[]" id="chbwpsr-<?php echo $pic->id; ?>"/>
				</th>
				<td class="WPSR_td_sel">
					<?php echo $pic->id ?>
				</td>
				<td class="WPSR_td_sel">
					<div class="alignleft">
					<img src="<?php echo WPSR_PATH."30x30_c/".$pic->path.$pic->file; ?>" alt="<?php echo $pic->file ?>" border="0" class="WPSR_aimg" />
					</div>
					<div class="alignleft">
						<strong><a href="#" onclick="WPSR_edit_pic(<?php echo $pic->id ?>,'WPSR_ed_pic_btn-<?php echo $pic->id ?>');blur();return false;" class="row-title"><?php echo $pic->file ?></a> [<a href="<?php echo get_option('home').'/'.WPSR_BASE_URI.'/show/'.$pic->id.'/'; ?>">^</a>]</strong><br />
						<?php echo $pic->width." x ".$pic->height." px"; ?>
					</div>
				</td>
				<td class="WPSR_td_sel">
					<span id="wpsr-cap-<?php echo $pic->id ?>"><?php echo $pic->caption; ?></span>
				</td>
				<td class="WPSR_td_sel">
					<strong><?php echo get_author_name($pic->owner); ?></strong>
				</td>
				<td class="WPSR_td_sel">
					<strong><span id="wpsr-lvl-<?php echo $pic->id ?>"><?php echo $pic->level ?></span></strong>
				</td>
				<td class="WPSR_td_sel">
					<strong><span id="wpsr-hits-<?php echo $pic->id ?>"><?php echo $pic->hits; ?></span></strong>
				</td>
				<td style="text-align:right;">
					<input class="button" type="button" name="edit-<?php echo $pic->id ?>" value="+ Edit" onclick="WPSR_edit_pic(<?php echo $pic->id ?>,this);blur();" id="WPSR_ed_pic_btn-<?php echo $pic->id ?>"/>
				</td>
			</tr>
			<tr class="WPSR_close" id="wpsr-edit-<?php echo $pic->id ?>">
				<td colspan="2">&nbsp;</td>
				<td colspan="6">
					<div id="wpsr-edit-res-<?php echo $pic->id ?>" />
				</td>
			</tr>
<?php
		}
?>
		</tbody>
		</table>

		</form>
<?php
	}





	/**
	 * adminManageOutput function.
	 * 
	 * @access public
	 * @param object $wpsr
	 * @param object $album
	 * @param int $faid
	 * @return void
	 */
	function adminManageOutput($wpsr, $album, $faid = 1) {
?>
		<h2>Showroom - Manage Galleries</h2>
		<div class="tablenav">
			<div class="alignleft">
				<form name="filter" method="get" action="" style="display:inline;" />
				<input name="event" type="hidden" value="filter" />
				<input type="hidden" name="page" value="WPshowroom/wpsr.php" />
				Filter: 
				<select id="faid" name="faid">
					<option value="1"><?php echo WPSR_BASE_NAME; ?></option>
<?php
		foreach ($album as $aid => $title) {
			echo "<option value='$aid'";
			if ($aid == $wpsr->obj->id) {
				echo " selected='selected'";
			}
			echo ">".$title."</option>";
		}
?>
				</select>
				<input id="" class="button-secondary" type="submit" name="album" value="Album"/> 
				<input id="" class="button-secondary" type="submit" name="picture" value="Pictures"/>
				</form>
			</div>
			<div class="alignright">
				<input class="button-secondary" type="button" name="addpic" value="+ Upload Pictures" onclick="WPSR_goto('<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=WPsr-pictures')"/>
				<input class="button-primary" type="button" name="addalb" id="WPSR_new_alb_btn" value="+ Add Album" onclick="WPSR_toggle_open('wpsr-new-alb');WPSR_toggle_current(this);blur();"/>
			</div>
		</div>
		<br class="clear"/>

		<table class="widefat">
			<thead>
				<tr>
					<th scope="col">ID</th>
					<th scope="col">Date</th>
					<th scope="col">Title</th>
					<th scope="col"># Pictures</th>
					<th scope="col">Owner</th>
					<th scope="col">Level</th>
					<th scope="col">
						<div class="vers">
							<img src="images/comment-grey-bubble.png" alt="Comments"/>
						</div>
					</th>
					<th scope="col">&nbsp;</th>
				</tr>
			</thead>
		<tbody>
			<tr class="WPSR_close WPSR_bbb" id="wpsr-new-alb">
				<td colspan="2">&nbsp;</td>
				<td colspan="6">
					<form name="album" method="post" action="../wp-content/plugins/WPshowroom/post.php" />
					<input name="event" type="hidden" value="album">
					<table border="0" cellspacing="5" cellpadding="0" width="100%" class="WPSR_tbledit">
						<tr>
							<td>
								<label for="title-0">Album Title:</label>
							</td>
							<td>
								<input class="WPSR_tblinput" id="title-0" type="text" value="" name="title-0"/><img src="images/required.gif" alt="required"/>
							</td>
							<td>
								<label for="comments-0">Comments:</label>
							</td>
							<td>
								<label for="comments-0">
									<input id="comments-0" type="checkbox" value="1" name="comments-0"/>
									Allow comments on pictures in this Album
								</label>
							</td>
						</tr>
						<tr>
							<td>
								<label for="desc-0">Description:</label>
							</td>
							<td>
								<input class="WPSR_tblinput" id="desc-0" type="text" value="" name="desc-0"/>
							</td>
							<td>
								<label for="level-0">Level:</label>
							</td>
							<td>
								<select id="level-0" name="level-0">
<?php
		for ($i=0; $i < 11; $i++) {
			echo "<option value='$i'>Userlevel $i</option>";
		}
?>
								</select>
							</td>
						</tr>
						<tr>
							<td>
								<label for="meta-0">Meta Information:</label>
							</td>
							<td>
								<input class="WPSR_tblinput" id="meta-0" type="text" value="" name="meta-0"/>
							</td>
							<td colspan="2">
								&nbsp;
							</td>						
						</tr>
						<tr>
							<td>
								<label for="parent-0">Parent Album:</label>
							</td>
							<td>
								<select id="parent-0" name="parent-0">
									<option value="1"><?php echo WPSR_BASE_NAME; ?></option>
<?php
		foreach ($album as $aid => $title) {
			echo "<option value='$aid'>".$title."</option>";
		}
?>
								</select><img src="images/required.gif" alt="required"/>
							</td>
							<td colspan="2">
								&nbsp;
							</td>
						</tr>
						<tr>
							<td colspan="2" align="left">
								&nbsp;
							</td>
							<td colspan="2" align="right">
								<input class="button-secondary" type="button" name="cancel" value=" Cancel " onclick="WPSR_toggle_open('wpsr-new-alb');WPSR_toggle_current('WPSR_new_alb_btn');blur();"/>
								<input class="button-primary" type="submit" name="submit" value=" Add Album "/>
							</td>
						</tr>
					</table>
					</form>
				</td>
			</tr>
<?php
		foreach ($wpsr->albums as $alb) {
			$intend = ($alb->intend - 1);
?>
			<tr id="wpsr-<?php echo $alb->id ?>" class="WPSR_tr">
				<td>
					<?php echo $alb->id ?>
				</td>
				<td>
					<abbr title="<?php echo $wpsr->getdate($alb->date); ?>"><?php echo $wpsr->getShortDate($alb->date); ?></abbr>
				</td>
				<td>
					<div class="alignleft">
					<img src="<?php echo $wpsr->getAlbCover($alb, "30x30_c"); ?>" style="width:30px;height:30px;" alt="Album <?php echo $alb->aid; ?>" border="0" class="WPSR_aimg" />
					</div>
					<div class="alignleft">
					<strong><a href="<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=WPshowroom/wpsr.php&event=filter&faid=<?php echo $alb->id; ?>" class="row-title"><?php echo str_repeat('—', $intend).' '.$alb->title; ?></a> [<a href="<?php echo get_option('home').'/'.WPSR_BASE_URI.'/'.$alb->uri.'/'; ?>">^</a>]</strong>
					</div>
				</td>
				<td>
					<a href="<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=WPshowroom/wpsr.php&wpsrdo=pictures&faid=<?php echo $alb->id ?>" class="row-title"><?php echo $alb->pics; ?></a>
				</td>
				<td>
					<strong><?php echo get_author_name($alb->owner); ?></strong>
				</td>
				<td>
					<strong><?php echo $alb->level ?></strong>
				</td>
				<td align="center">
<?php
			if ($alb->com == 1) {
?>
						<img src="images/yes.png" alt="Comments"/>
<?php
			}
			else {
?>
						<img src="images/no.png" alt="Comments"/>
<?php
			}
?>
				</td>
				<td style="text-align:right;">
					<form name="up-<?php echo $alb->id ?>" method="post" action="../wp-content/plugins/WPshowroom/post.php" style="display:inline;" />
						<input name="event" type="hidden" value="moveup">
						<input name="id" type="hidden" value="<?php echo $alb->id ?>">
						<input name="paid" type="hidden" value="<?php echo $alb->paid ?>">
						<input name="faid" type="hidden" value="<?php echo $faid ?>">
						<input class="button" type="submit" name="up" value="^"/>
					</form>
					<form name="down-<?php echo $alb->id ?>" method="post" action="../wp-content/plugins/WPshowroom/post.php" style="display:inline;" />
						<input name="event" type="hidden" value="movedown">
						<input name="id" type="hidden" value="<?php echo $alb->id ?>">
						<input name="paid" type="hidden" value="<?php echo $alb->paid ?>">
						<input name="faid" type="hidden" value="<?php echo $faid ?>">
						<input class="button" type="submit" name="down" value="v"/>
					</form>
					<input class="button" type="button" name="edit-<?php echo $alb->id ?>" value="+ Edit" onclick="WPSR_edit_alb(<?php echo $alb->id ?>,this);blur();" id="WPSR_ed_alb_btn-<?php echo $alb->id ?>"/>
				</td>
			</tr>
			</tr>
			<tr class="WPSR_close" id="wpsr-edit-<?php echo $alb->id ?>">
				<td colspan="2">&nbsp;</td>
				<td colspan="6">
					<div id="wpsr-edit-res-<?php echo $alb->id ?>"></div>
				</td>
			</tr>
<?php
		}
?>
		</tbody>
		</table>
<?php
	}





	/**
	 * adminPictureOutput function.
	 * 
	 * @access public
	 * @param int $user_ID
	 * @param object $files
	 * @param object $wfiles
	 * @return void
	 */
	function adminPictureOutput($user_ID, $files, $wfiles) {
		if ($_GET['wpsrdo'] == 'insert') {
?>
			<h2>Showroom - Insert Pictures</h2>
			<div style="height:20px;" id="insertdummy">&nbsp;</div>
			<div id="wpsr-upload" style="height:600px;">
				<?php $this->adminInsertPics("new"); ?>
			</div>
<?php
		}
		elseif ($_GET['wpsrdo'] == 'regenerate') {
?>
			<h2>Showroom - Regenerate Pictures</h2>
			<div style="height:20px;" id="insertdummy">&nbsp;</div>
			<div id="wpsr-upload" style="height:600px;">
				<?php $this->adminInsertPics("reg"); ?>
			</div>
<?php
		}
		else {
?>
			<h2>Showroom - Upload Pictures</h2>
			<div style="height:20px;display:none;" id="insertdummy">&nbsp;</div>
			<div id="wpsr-upload" style="height:600px;">
				<?php $this->adminFileListOutput($user_ID, $files, $wfiles); ?>
			</div>
<?php
		}
	}





	/**
	 * adminFileListOutput function.
	 * 
	 * @access public
	 * @param int $user_ID
	 * @param object $files
	 * @param object $wfiles
	 * @return void
	 */
	function adminFileListOutput($user_ID, $files, $wfiles) {
?>
		<form id="assign-pics" method="post" action="../wp-content/plugins/WPshowroom/post.php">

		<div class="tablenav">
			<div class="alignleft actions">
				<select style="float:left; margin-right:5px;" name="action" onchange="WPSR_apoptions(this.options[this.selectedIndex].value);blur();return false;">
					<option selected="selected" value="-1">Bulk Actions</option>
					<option value="assign">Assign Pictures</option>
					<option value="unassign">Unassign Pictures</option>
					<option value="delete">Delete Pictures</option>
				</select>
				<div id="wpsr-alblist" style="float:left;height:20px;width:650px;">
					&nbsp;
				</div>
			</div>
		</div>

		<br class="clear"/>

		<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<tr>
		<td valign="top">
			<table class="widefat">
				<thead>
					<tr>
						<th id="cb" class="manage-column column-cb check-column" style="" scope="col">
							<input type="checkbox"/>
						</th>
						<th scope="col" style="width:16px;">&nbsp;</th>
						<th scope="col">Files pending for <?php echo get_author_name($user_ID); ?></th>
						<th scope="col">waiting for</th>
						<th scope="col" style="width:100px;text-align:right;">Picture Size</th>
						<th scope="col" style="width:100px;text-align:right;">File Size</th>
						<th scope="col" style="width:20px;">&nbsp;</th>
					</tr>
				</thead>
			<tbody style="height:500px;overflow:auto;">

<?php
		if (count($files) < 1 && count($wfiles) < 1) {
?>
				<tr>
					<td colspan="6" style="border-bottom:none">no pictures uploaded.</td>
				</tr>
<?php
		}
		else {
			if (count($wfiles) > 0) {
				foreach ($wfiles as $wfile) {
?>
				<tr class="WPSR_tr WPSR_tr_sel" id="wpsrwf-<?php echo $wfile->id; ?>">
					<th class="check-column" scope="row">
						<input type="checkbox" value="<?php echo $wfile->id; ?>" name="file[]" id="chbwpsrwf-<?php echo $wfile->id; ?>" />
					</th>
					<td class="WPSR_td_sel"><img src="images/yes.png" alt="assigned"/></td>
					<td class="WPSR_td_sel"><a href="#" onclick="WPSR_load_thumb('<?php echo $wfile->file; ?>','0');return false;"><?php echo $wfile->file; ?></a></td>
					<td class="WPSR_td_sel">Album: <?php echo $wfile->album; ?> | Level: <?php echo $wfile->level; ?></td>
					<td class="WPSR_td_sel" style="text-align:right;"><?php echo $wfile->xsize." x ".$wfile->ysize; ?> px</td>
					<td class="WPSR_td_sel" style="text-align:right;"><?php echo $wfile->fsize; ?></td>
				</tr>
<?php
				}
			}
			if (count($files) > 0) {
				foreach ($files as $file) {
?>
				<tr class="WPSR_tr WPSR_tr_sel" id="wpsrf-<?php echo $file['fn']; ?>">
					<th class="check-column" scope="row">
						<input type="checkbox" value="<?php echo $file['fn']; ?>" name="filename[]" id="chbwpsrf-<?php echo $file['fn']; ?>"/>
					</th>
					<td class="WPSR_td_sel"><img src="images/marker.png" alt="assigned"/></td>
					<td class="WPSR_td_sel"><a href="#" onclick="WPSR_load_thumb('<?php echo $file['fn']; ?>','1');return false;"><?php echo $file['fn']; ?></a></td>
					<td class="WPSR_td_sel">--</td>
					<td class="WPSR_td_sel" style="text-align:right;"><?php echo $file['xsize']." x ".$file['ysize']; ?> px</td>
					<td class="WPSR_td_sel" style="text-align:right;"><?php echo $file['fsize']; ?></td>
				</tr>
<?php
				}
			}
		}
?>
				<tr><td colspan="6" style="border-bottom:none;">&nbsp;</td></tr>
			</tbody>
			</table>
		</td>
		<td style="padding-left:10px;width:300px;" valign="top">
			<table class="widefat">
				<thead>
					<tr>
						<th scope="col">Picture Preview</th>
					</tr>
				</thead>
			<tbody>
				<tr>
					<td style="text-align:center;border-bottom:none;">
						<div style="border:1px solid #CCC; width:200px; height:200px;display:inline-block;" id="thumbcont">
							<div style="text-align:center;margin-top:90px"><em>select file</em></div>
						</div>
					</td>
				</tr>
			</tbody>
			</table>
			<br />
			<table class="widefat">
				<thead>
					<tr>
						<th scope="col">Upload Pictures</th>
					</tr>
				</thead>
			<tbody>
				<tr>
					<td style="border-bottom:none">
						<input class="button-primary" type="button" name="addpic" value="+ Upload Pictures" onclick="WPSR_loadwomsg_div('wpsr-upload',1);"/>
					</td>
				</tr>
			</tbody>
			</table>
			<br />
			<table class="widefat">
				<thead>
					<tr>
						<th scope="col">Insert Pictures</th>
					</tr>
				</thead>
			<tbody>
				<tr>
					<td style="border-bottom:none">
						<strong><?php echo count($wfiles); ?> Pictures waiting for processing</strong>
						<br /><br />
						--> <a href="admin.php?page=WPsr-pictures&wpsrdo=insert">Insert Pictures</a>
						<br />
						--> <a href="admin.php?page=WPsr-pictures&wpsrdo=regenerate">Regenerate Thumbnails</a>
						<br /><br />
					</td>
				</tr>
			</tbody>
			</table>
		</td>
		</tr>
		</table>

		</form>
<?php
	}


}


?>